package com.cx.base.domain;

import com.cx.base.rx.DefObserver;
import com.cx.util.Preconditions;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public abstract class UseCase<T, Q extends UseCase.RequestValues> {


    protected abstract Observable<T> buildUseCaseObservable(Q params);

    public void execute(Q params, DefObserver<T> observer) {
        Preconditions.checkNotNull(observer);
        buildUseCaseObservable(params)
                .compose(new ObservableTransformer<T, T>() {
                    @Override
                    public ObservableSource<T> apply(Observable<T> upstream) {
                        return upstream
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread());
                    }
                })
                .subscribe(observer);
    }


    public interface RequestValues {
    }


}
